package com.cloudkinto.controller.api;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.api.*;
import com.cloudkinto.bo.order.inbound.OrderInputQueryBo;
import com.cloudkinto.bo.order.outbound.NormalOrderOutboundApiQueryBo;
import com.cloudkinto.bo.order.outbound.NormalOrderOutboundQueryBo;
import com.cloudkinto.bo.order.outbound.TransferOrderOutboundQueryBo;
import com.cloudkinto.bo.product.ProductQueryBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dto.order.outbound.UpdateSendWmsApiReq;
import com.cloudkinto.dto.order.outbound.UpdateTrackingNoImportWmsApiDto;
import com.cloudkinto.service.company.CompanyService;
import com.cloudkinto.service.order.input.OrderInputService;
import com.cloudkinto.service.order.outbound.OutboundImportExportService;
import com.cloudkinto.service.order.outbound.OutboundService;
import com.cloudkinto.service.order.outbound.vo.OrderOutboundApiExceptionReq;
import com.cloudkinto.service.order.outbound.vo.OutboundUpdateStatusReq;
import com.cloudkinto.service.order.shelf.OrderInputShelfService;
import com.cloudkinto.service.product.ProductService;
import com.cloudkinto.service.stock.ProductStockService;
import com.cloudkinto.service.user.UserService;
import com.cloudkinto.vo.api.OrderInputApiListVo;
import com.cloudkinto.vo.api.ProductApiVo;
import com.cloudkinto.vo.api.TransferOutboundApiVo;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import com.cloudkinto.vo.company.CompanyRes;
import com.cloudkinto.vo.order.outbound.NormalOutboundVo;
import com.cloudkinto.vo.stock.adjust.box.AdjustItemRes;
import com.cloudkinto.vo.user.UserLoginReq;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author long
 * @Description 开放api
 * @Date 2025/7/3 9:59
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Api开放接口")
@RequestMapping("/api/admin/third")
public class ThirdApiController extends BaseController {

    private final OutboundService outboundService;
    private final UserService userService;
    private final OutboundImportExportService outboundImportExportService;
    private final OrderInputService orderInputService;
    private final OrderInputShelfService orderInputShelfService;
    private final ProductService productService;
    private final ProductStockService productStockService;
    private final CompanyService companyService;


    @PostMapping("/wms/order/outbound/login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public SingleResult login(@RequestBody @Validated UserLoginReq req, HttpServletRequest request) {
        return SingleResult.success("登录成功", userService.login(req, request, "api"));
    }

    @UserSessionRequired
    @PostMapping("/wms/order/outbound/getNormalListPage")
    @ApiOperation(value = "一件代发出库单查询（分页）", notes = "一件代发出库单查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = NormalOutboundVo.class))
    public PageResult<NormalOutboundVo> getNormalListPage(@RequestBody @Validated NormalOrderOutboundApiQueryBo bo) {
        NormalOrderOutboundQueryBo queryBo = new NormalOrderOutboundQueryBo();
        BeanUtils.copyProperties(bo, queryBo);
        queryBo.setTenantId(getTenantId());
        queryBo.setSortOrder("DESC");
        queryBo.setUserId(getCurrentUserId());
        queryBo.setSource("wms");
        queryBo.setWhId(getWarehouseId());
        queryBo.setExceptionSource(1);
        queryBo.setApiFlag(true);
        //只获取待处理
//        queryBo.setStatus(NormalOutboundStatusEnum.one.getValue());
        return outboundService.getNormalListPageApi(queryBo);
    }

    @UserSessionRequired
    @PutMapping("/wms/order/outbound/importUpdateTrackingNumber")
    @ApiOperation(value = "导入更新跟踪号", notes = "导入更新跟踪号")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult importUpdateTrackingNumber(@RequestBody @Validated UpdateTrackingNoImportWmsApiDto req) {
        return SingleResult.success(outboundImportExportService.importUpdateTrackingNumberApi(req.getData(), req.getOrderType(), getTenantId(), getCurrentUserId(), "api", getWarehouseId()));
    }


    @UserSessionRequired
    @PutMapping("/wms/order/outbound/consumerOutboundSend")
    @ApiOperation(value = "订单出库", notes = "订单出库")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功"))
    public SingleResult importUpdateTrackingNumber(@RequestBody @Validated UpdateSendWmsApiReq req) {
        List<String> success = outboundService.consumerJdyOutboundAutoSendByNo(req.getOutboundNoList(), req.getOrderType(), getWarehouseId(), getCurrentUserId(), "api");
        return SingleResult.success(success);
    }


    @UserSessionRequired
    @PutMapping("/wms/order/outbound/dealError")
    @ApiOperation(value = "订单出库不扣库存", notes = "订单出库不扣库存")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功"))
    public SingleResult dealError(@RequestBody @Validated List<Long> ids) {
        outboundService.dealError(ids, null);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("/wms/order/transfer/outboundSend")
    @ApiOperation(value = "批发单出库")
    public SingleResult transferOutboundOperationApi(@RequestBody @Validated TransferSendApiBo bo) {
        return outboundService.transferOutboundOperationApi(bo, getCurrentUserId(), "invoke api");
    }

    @UserSessionRequired
    @PutMapping("/wms/order/outbound/batchUpdateStatus")
    @ApiOperation(value = "更新出库单状态")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功"))
    public SingleResult batchUpdateStatus(@RequestBody @Validated OutboundUpdateStatusReq req) {
        List<String> success = outboundService.batchUpdateStatus(req.getOutboundNoList(), req.getStatus(), req.getOrderType(), getWarehouseId(), getCurrentUserId(), "api");
        return SingleResult.success(success);
    }

    @UserSessionRequired
    @PutMapping("/wms/order/outbound/exceptionOrder")
    @ApiOperation(value = "标记异常订单", notes = "标记异常订单")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功"))
    public SingleResult batchUpdateStatus(@RequestBody @Validated OrderOutboundApiExceptionReq req) {
        List<String> list = outboundService.exceptionOrder(req.getOutboundNoList(), req.getExceptionType(), req.getExceptionReason(), req.getOrderType(), getWarehouseId(), getCurrentUserId());
        return SingleResult.success(list);
    }

    @UserSessionRequired
    @PostMapping("/wms/order/outbound/getTransferListPage")
    @ApiOperation(value = "批发单分页查询")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = TransferOutboundApiVo.class))
    public PageResult<TransferOutboundApiVo> getTransferListPage(@RequestBody @Validated TransferOrderOutboundApiQueryBo bo) {
        TransferOrderOutboundQueryBo queryBo = new TransferOrderOutboundQueryBo();
        BeanUtils.copyProperties(bo, queryBo);
        queryBo.setWhId(getWarehouseId());
        queryBo.setTenantId(getTenantId());
        queryBo.setUserId(getCurrentUserId());
        queryBo.setSource("wms");
        queryBo.setExceptionSource(1);
        queryBo.setApiFlag(true);
        return outboundService.getTransferListPageApi(queryBo);
    }

    @UserSessionRequired
    @PostMapping("/wms/order/input/listPage")
    @ApiOperation(value = "入库单分页查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = OrderInputApiListVo.class))
    public PageResult listPage(@RequestBody @Validated OrderInputApiQueryBo bo) {
        OrderInputQueryBo queryBo = new OrderInputQueryBo();
        BeanUtils.copyProperties(bo, queryBo);
        queryBo.setWhId(getWarehouseId());
        queryBo.setTenantId(getTenantId());
        queryBo.setUserId(getCurrentUserId());
        queryBo.setSource("wms");
        return orderInputService.getListPageApi(queryBo);
    }

    /**
     * 常规入库和批发入库类型都可调用，仅适用于待入库状态的入库单，进行整单收货并上架
     */
    @UserSessionRequired
    @PostMapping("/wms/order/input/wholeOrderReceiptAndShelf")
    @ApiOperation(value = "根据入库单号整单收货并上架")
    @ApiResponse(code = 200, message = "上架成功")
    public SingleResult wholeOrderReceiptAndShelf(@RequestBody @Validated InboundReceiptAndShelfApiBo bo) {
        orderInputShelfService.wholeOrderReceiptAndShelf(bo, getCurrentUserId(), "api");
        return SingleResult.success("上架成功");
    }

    /**
     * 供常规入库类型入库单按箱唛收货并上架
     */
    @UserSessionRequired
    @PostMapping("/wms/order/input/receiptAndShelf")
    @ApiOperation(value = "常规入库入库单按箱唛收货并上架")
    public SingleResult normalReceiptAndShelfApi(@RequestBody @Validated NormalReceiptAndShelfApiBo bo) {
        orderInputService.normalReceiptAndShelfApi(bo, getCurrentUserId(), "api");
        return SingleResult.success("操作成功");
    }

    /**
     * 供常规入库类型入库单按sku收货并上架
     */
    @UserSessionRequired
    @PostMapping("/wms/order/input/receiptAndShelfBySku")
    @ApiOperation(value = "常规入库入库单按sku收货并上架")
    public SingleResult normalReceiptAndShelfBySkuApi(@RequestBody @Validated NormalReceiptAndShelfBySkuApiBo bo) {
        orderInputService.normalReceiptAndShelfBySkuApi(bo, getCurrentUserId(), "api");
        return SingleResult.success("操作成功");
    }

    @UserSessionRequired
    @PutMapping("/wms/order/input/batchUpdateStatus")
    @ApiOperation(value = "更新入库单状态")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功"))
    public SingleResult batchUpdateInputStatus(@RequestBody @Validated InputUpdateStatusBo bo) {
        List<String> success = orderInputService.batchUpdateInputStatus(bo, getWarehouseId(), getCurrentUserId(), "api");
        return SingleResult.success(success);
    }

    @UserSessionRequired
    @PostMapping("/wms/product/listPage")
    @ApiOperation(value = "产品分页查询")
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功", response = ProductApiVo.class))
    public PageResult listPage(@RequestBody ProductApiQueryBo bo) {
        ProductQueryBo queryBo = new ProductQueryBo();
        BeanUtils.copyProperties(bo, queryBo);
        queryBo.setTenantId(getTenantId());
        queryBo.setUserId(getCurrentUserId());
        queryBo.setSource("wms");
        //仅显示审核通过的产品
        queryBo.setApproveStatus(2);
        return productService.getListPageApi(queryBo);
    }


    @UserSessionRequired
    @GetMapping("/getCompanyList")
    @ApiOperation(value = "获取公司列表", notes = "")
    @ApiImplicitParams({
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = CompanyRes.class))
    public SingleResult getCompanyList() {
        return SingleResult.success("查询成功", companyService.getCompanyList(getCurrentUserId(), getTenantId(), "wms"));
    }


    @UserSessionRequired
    @PostMapping("/wms/ProductStock/skuStockAdjust")
    @ApiOperation(value = "产品库存-库存调整")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = AdjustItemRes.class))
    public SingleResult skuStockAdjust(@RequestBody @Validated SkuAdjustApiBo req) {
        List<AdjustItemRes> adjustItemRes = productStockService.skuStockAdjustApi(req, getWarehouseId(), getCurrentUserId(), getTenantId("wms"));
        return SingleResult.success(adjustItemRes);
    }


}
